package com.kanshu.ksgb.fastread.doudou.module.download.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.kanshu.ksgb.fastread.doudou.module.download.bean.DownLoadChapterBean;
import com.kanshu.ksgb.fastread.doudou.module.download.utils.TreeHelper;


import java.util.ArrayList;
import java.util.List;

/**
 *
 * @param <T>
 * @author zhy
 */
public abstract class TreeListViewAdapter<T> extends BaseAdapter {

    protected Context mContext;

    protected List<DownLoadChapterBean> mNodes;

    /**
     * 存储所有的Node
     */
    protected List<DownLoadChapterBean> mAllNodes = new ArrayList<>();


    protected LayoutInflater mInflater;

    protected List<Integer> lst = new ArrayList<Integer>();//已展开节点位置的集合
    public TreeListViewAdapter(ListView mTree, Context context, List<DownLoadChapterBean> datas){

        mAllNodes = datas;

        // 过滤出可见的Node
        mNodes = TreeHelper.getRootNodes(datas);
        mContext = context;
        mInflater = LayoutInflater.from(context);

        // 设置节点点击时，可以展开以及关闭；并且将ItemClick事件继续往外公布
        mTree.setOnItemClickListener((parent, view, position, id) -> {
            expandOrCollapse(position);
        });
    }


    public void setData(List<DownLoadChapterBean> datas){
        mAllNodes = datas;
        // 过滤出可见的Node
        mNodes = TreeHelper.getRootNodes(datas);
        notifyDataSetChanged();
    }

    /**
     * 相应ListView的点击事件 展开或关闭某节点
     */
    private void expandOrCollapse(int position) {
        DownLoadChapterBean bean = mNodes.get(position);
        if (bean != null && !TextUtils.isEmpty(bean.id)) {// 排除传入参数错误异常
            bean.is_show = !bean.is_show;
            mNodes = TreeHelper.filterVisibleNode(mAllNodes);
            notifyDataSetChanged();// 刷新视图
        }
    }

    /**
     * 关闭所有展开节点
     */
    public void closeAllNode() {
        for (int i = mAllNodes.size() - 1; i >= 0; i--) {
            DownLoadChapterBean bean = mAllNodes.get(lst.get(i));
            if (bean != null) {// 排除传入参数错误异常
                bean.is_show = false;
            }
            mNodes = TreeHelper.getRootNodes(mAllNodes);
        }
        notifyDataSetChanged();// 刷新视图
        lst.clear();
    }

    @Override
    public int getCount() {
        return mNodes.size();
    }

    @Override
    public Object getItem(int position) {
        return mNodes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DownLoadChapterBean bean = mNodes.get(position);
        convertView = getConvertView(bean, position, convertView, parent);
        return convertView;
    }

    public abstract View getConvertView(DownLoadChapterBean node, int position,
                                        View convertView, ViewGroup parent);

}
